<template>
  <div class="department-container">
    <div class="app-container">
      <!-- 卡片 -->
      <el-card>
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" />
            <span> 江苏传智播客教育科技股份有限公司 </span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col> 负责人 </el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <!-- <el-dropdown> -->
                <span>操作</span>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树状结构     :props="defaultProps"-->
        <el-tree default-expand-all :data="list">
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="peo" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>{{ data.manager }}</el-col>
                </el-row>
              </el-col>
              <el-dropdown>
                <span>操作<i class="el-icon-arrow-down" /> </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    @click.native="hAdd(data.id)"
                  >添加子部门</el-dropdown-item>
                  <el-dropdown-item @click.native="hEdit(data.id)" @click="isEdit=true">编辑部门</el-dropdown-item>
                  <el-dropdown-item
                    @click.native="hDel(data.id)"
                  >删除部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog :title="isEdit ?'编辑':'添加'" :visible.sync="showDialog" width="50%" center>
      <AddorEdit
        v-if="showDialog"
        :pid="curId"
        :is-edit="isEdit"
        :existed-code-list="existedCodeList"
        :c-existed-name-list="cExistedName"
        @update-depart="hUpdateDepart"
        @closeFn="closefn"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddorEdit from './AddorEdit'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      existedCodeList: [], // 最新获取code编码  形式为['sw','2','d3'],传到子组件去
      isEdit: false, // 是否处于编辑状态   false不是编辑状态
      showDialog: false, // 弹框出现
      curId: '',
      list: [
        // {
        //   name: '财务部',
        //   children: [
        //     {
        //       name: '财务核算部'
        //     },
        //     {
        //       name: '税务核算部'
        //     }
        //   ]
        // }
      ],
      originList: [] // 同级部门数组里面有id pid  name
    }
  },
  computed: {
    // 需要用到id pid ,name  当pid 等于 curid 时候是在字级下不能重名
    // this.curId
    // originList={id,pid,name }
    cExistedName() {
      if (this.isEdit) {
        const parent = this.originList.find(item => item.id === this.curId)
        const pid = parent.pid
        const rs = this.originList.filter(item => item.pid === pid).map(item => item.name)
        console.log('找到同级部门名字', rs)
        return rs
      } else {
        return this.originList.filter(item => item.pid === this.curId).map(item => item.name)
      }
    }
  },

  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      const res = await getDepartments()
      console.log('部门信息', res.data.depts)
      this.existedCodeList = res.data.depts.map(item => item.code)
      // console.log('code编码', this.existedCodeList)
      // 获取到部门的code编码
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          name: item.name
        }
      })
      console.log(this.originList) // [id,pid,name]
      this.list = await tranListToTreeData(res.data.depts)
      // console.log('更改完树状结构', list)
    },
    // 添加部门
    hAdd(Id) {
      this.showDialog = true
      this.curId = Id
      this.isEdit = false
    },
    // 收到子组件中的update-depart事件
    hUpdateDepart() {
      this.showDialog = false
      this.loadDepartments()
    },
    // 修改列表
    hEdit(Id) {
      this.showDialog = true
      this.curId = Id
      this.isEdit = true
      this.loadDepartments()
    },
    closefn() {
      this.showDialog = false
    },
    // 删除部门
    hDel(Id) {
      this.$confirm('此操作将永久删除该部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delDepartment(Id)
          console.log('删除部门', res)
          this.loadDepartments()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
